package com.butterfly.hotkey.common.result;

import com.butterfly.hotkey.common.exception.IException;

/**
 * @author butterfly
 */
public class Result<T> {

    /**
     * 默认的成功
     */
    private static final int DEFAULT_SUCCESS_CODE = 200;

    /**
     * 默认的成功信息
     */
    private static final String DEFAULT_SUCCESS_MSG = "Success";

    /**
     * 编码
     */
    private Integer code;

    /**
     * 编码
     */
    private String message;

    /**
     * 追踪ID
     */
    private String requestId;

    /**
     * 数据
     */
    private T data;

    public Result() {
        this.code = DEFAULT_SUCCESS_CODE;
        this.message = DEFAULT_SUCCESS_MSG;
    }

    public Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> Result<T> error(IException e) {
        return new Result<>(e.getCode(), e.getMessage(), null);
    }

    public static <T> Result<T> error(Integer code, String message) {
        return new Result<>(code, message, null);
    }

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(DEFAULT_SUCCESS_CODE, DEFAULT_SUCCESS_MSG, data);
    }


}
